//
//  HomeTableViewController.swift
//  HJWeibo
//
//  Created by huangjiong on 16/1/31.
//  Copyright © 2016年 huangjiong. All rights reserved.
//

import UIKit

let HomeReuseIdentifier = "HomeReuseIdentifier"
class HomeTableViewController: BaseTableViewController {
    
    var statuses: [Statuses]? {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.如果没有登录, 就设置未登录界面的信息
        if userLogin == false
        {
            visitorView?.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
        }
        //已经登录了的话,就再设置导航条
        else
        {
            setupNavBar()
        }
        
        //监听通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "menuViewChange", name: kHomeMenuWillShow, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "menuViewChange", name: kHomeMenuWillDismiss, object: nil)
        
        //注册cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeReuseIdentifier)
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        //获取网络数据
        Statuses.loadStatuses { (models, error) -> () in
            if error != nil
            {
                return
            }
            self.statuses = models
        }
    }
    
    func menuViewChange()
    {
        let btn = navigationItem.titleView as! HomeTitleButton
        //1.修改选中状态,以切换箭头
        btn.selected = !btn.selected
    }
    
    
    private func setupNavBar()
    {
        //1.左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.createBarButtonItem(imageName:"navigationbar_friendattention", target: self, action: "leftBtnClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem.createBarButtonItem(imageName:"navigationbar_pop", target: self, action: "rightBtnClick")
        
        //2.中间标题按钮
        let titleBtn = HomeTitleButton()
        titleBtn.setTitle("红胖子没死 ", forState: UIControlState.Normal)
        titleBtn.addTarget(self, action: "titleBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleBtn
    }
    
    
   // MARK: - 导航栏中间标题按钮点击事件
    func titleBtnClick(btn: HomeTitleButton)
    {
        
        //2弹出菜单
        let vc = PopoverViewController()
        popoverAnimator.presentFrame = CGRect(x: 100, y: 56, width: 200, height: 350)
        vc.transitioningDelegate = popoverAnimator
        vc.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        presentViewController(vc, animated: true, completion: nil)
                
    }

   // MARK: - 导航栏左右按钮点击事件
    func leftBtnClick()
    {
        print("点击左按钮")
    }
    //点击扫描二维码
    func rightBtnClick()
    {
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        presentViewController(vc!, animated: true, completion: nil)
    }
    

    

    
    //MARK: - 定义一个属性,记录当前是否展开
    var isPresent: Bool = false
    //MARK: - 懒加载一个属性,用于保存负责转场的对象
    lazy var popoverAnimator: PopoverAnimator = {
       let pop = PopoverAnimator()
        return pop
    }()
    
   
    
}


extension HomeTableViewController
{
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return statuses?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeReuseIdentifier) as! StatusCell
        let model = statuses![indexPath.row]
        cell.status = model
        return cell
    }
}



